home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_10 / milam / datesupp.c < prev    next >
C/C++ Source or Header  |  1994-07-12  |  5KB  |  156 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /*              (c) Copyright 1993 by Stan Milam.              */
  4. /*                                                             */
  5. /***************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <ctype.h>
  11. #include "dates.h"
  12.  
  13. #define MAXDATE 3652059L               /* 31-Dec-9999 */
  14.  
  15. /***************************************************************/
  16. /* A table which contains an accumulation of days of all pre-  */
  17. /* ceeding months.                                             */
  18. /***************************************************************/
  19.  
  20. static int month_accum_table[] = {
  21.       0,  31,  59,  90, 120, 151,
  22.     181, 212, 243, 273, 304, 334
  23. };
  24.  
  25. /***************************************************************/
  26. /* Each element contains the normal number of days for each    */
  27. /* month.                                                      */
  28. /***************************************************************/
  29.  
  30. static int month_table[] = {
  31.     31, 28, 31, 30, 31, 30,
  32.     31, 31, 30, 31, 30, 31
  33. };
  34.  
  35. /***************************************************************/
  36. /* The full names of the weekdays.                             */
  37. /***************************************************************/
  38.  
  39. static char *full_weekday[] = {
  40.     "Sunday", "Monday", "Tuesday",
  41.     "Wednesday", "Thursday", "Friday", "Saturday"
  42. };
  43.  
  44. /***************************************************************/
  45. /* The full names of the months.                               */
  46. /***************************************************************/
  47.  
  48. static char *full_month[] = {
  49.     "January", "February", "March", "April", "May", "June",
  50.     "July", "August", "September", "October", "November", "December"
  51. };
  52.  
  53. static int compare( char *s1, char *s2, unsigned len ) {
  54.  
  55.     unsigned ch1, ch2;
  56.  
  57.     while ( *s1 && *s2 && len ) {
  58.         ch1 = toupper(*s1);
  59.         ch2 = toupper(*s2);
  60.         if (ch1 < ch2)
  61.             return -1;
  62.         else if ( ch1 > ch2 )
  63.             return 1;
  64.         else s1++, s2++, len--;
  65.     }
  66.     if ( len == 0 ) return 0;
  67.     else if (*s1) return 1;
  68.     else return  -1;
  69. }
  70.  
  71. static int is_it_a_leap_year( unsigned year ) {
  72.  
  73.     return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 );
  74. }
  75.  
  76. static date_t years_to_days( unsigned year ) {
  77.  
  78.     date_t rv;
  79.  
  80.     if (year > 0) year--;
  81.     rv = year * 365L + year / 4L - year / 100L + year / 400L;
  82.     return rv;
  83. }
  84.  
  85. static date_t months_to_days( int month, int leap_year ) {
  86.  
  87.     date_t rv;
  88.  
  89.     rv = month_accum_table[month - 1];
  90.     if ( month > 2 ) rv += leap_year;
  91.     return rv;
  92. }
  93.  
  94. static int days_to_months( int *days, int leap_year ) {
  95.  
  96.     int rv, month, save_month;
  97.  
  98.     save_month = month_table[1];
  99.     month_table[1] += leap_year;
  100.     for ( month = 0 ;; month++ ) {          /* Go find the     */
  101.                                             /* month and day   */
  102.         if ( *days > month_table[month] )   /* More days than  */
  103.                                             /* in month?       */
  104.             *days -= month_table[month];    /* Yes, subtract   */
  105.                                             /* from days       */
  106.         else {
  107.             rv = month;                     /* Found month,    */
  108.                                             /* days left over  */
  109.             break;                          /* Get out of loop */
  110.         }
  111.     }
  112.     month_table[1] = save_month;
  113.     return rv;                              /* Return month    */
  114. }
  115.  
  116. /***************************************************************/
  117. /*                        week_of_year()                       */
  118. /*                                                             */
  119. /* NOTE! This function was borrowed from P. J. Plauger's book  */
  120. /* "The Standard C Library".                                   */
  121. /*                                                             */
  122. /***************************************************************/
  123.  
  124. static int week_of_year( int start, int wday, int yday ) {
  125.  
  126.     wday = ( wday + 7 - start ) % 7;
  127.     return ( yday - wday + 12 ) / 7 - 1;
  128. }
  129.  
  130. date_t time_to_date( time_t tv ) {
  131.  
  132.     date_t rv;
  133.     struct tm *tm;
  134.     int    year, leap_year;
  135.  
  136.     /***********************************************************/
  137.     /* Get a time structure to use for conversion process.     */
  138.     /***********************************************************/
  139.  
  140.     tm = localtime(&tv);
  141.  
  142.     /***********************************************************/
  143.     /* Use values in the tm structure to convert the current   */
  144.     /* date into a long integer value.                         */
  145.     /***********************************************************/
  146.  
  147.     year = tm -> tm_year + 1900;
  148.     leap_year = is_it_a_leap_year(year);
  149.     rv  = years_to_days( year );
  150.     rv += months_to_days(tm -> tm_mon + 1, leap_year );
  151.     rv += tm -> tm_mday;
  152.  
  153.     return rv;
  154. }
  155.  
  156.